#include <bits/stdc++.h>

#define fi first
#define se second
#define eb emplace_back
#define ep emplace
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

const int N = 1e5 + 10;
const ll inf = 1e12;
const int M = 510 * 510 * 4;

template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

int n;
int a[510][510];

void solve() {
	scanf("%d", &n); rep(i, 1, n) rep(j, 1, n)  scanf("%d", &a[i][j]);
	ll ans = 1e18, ret = 0;
	rep(i, 1, n) {
		vec pot;
		rep(j, 1, n) pot.eb(a[i][j]);
		sort(pot.begin(), pot.end());
		bool fl = 0;
		for(auto v : pot) {
			if(fl && v >= 0) break;
			ret += v; fl = 1;
		}
	} chkmin(ans, ret);

	ret = 0;
	rep(i, 1, n) {
		vec pot;
		rep(j, 1, n) pot.eb(a[j][i]);
		sort(pot.begin(), pot.end());
		bool fl = 0;
		for(auto v : pot) {
			if(fl && v >= 0) break;
			ret += v; fl = 1;
		}
	} chkmin(ans, ret);
	
	printf("%lld\n", ans);
}
int main() {
#ifdef YJR_2333_TEST
	freopen("1.in", "r", stdin);
#endif
	//ios :: sync_with_stdio(false), cin.tie(0), cout.tie(0);
	int T; scanf("%d", &T);
	while(T--) solve();
}
